Dynomotion

Group: DynoMotion Message: 15104 From: moray_cuthill Date: 10/20/2017
Subject: Displaying doubles/floats in a message box
As part of my tool changer initialisation, I display a couple text boxes to confirm positions/tools. However, I can't get partial positions to display properly.

Here's the relevant code snippet-

char MyMessage[80];  // String to be created and displayed
     double CrslP = *CrslPos/4;
     printf("CrslPos = %d\n", *CrslPos);
     printf("CrslP = %.1f\n", CrslP);
     sprintf(MyMessage,"Is carousel at position %.1f",CrslP);  // build the message we want to show
     MsgBoxNoWait(MyMessage,MB_YESNO|MB_ICONEXCLAMATION);
     
*CrslPos is an integer/uservar that contains the carousel position (the numbered position gets multiplied by 4, as it runs on a Geneva mechanism with 2 rotation between each tool holder, and multiplyin by 4 allows me to track position, and use remainder calculations to test for a valid position)

What I'd like is when the carousel is at the mid-point between two holders, I want the message box to show x.5, however the fractional is shown as 0.
I.e. if the carousel is between 1 and 2, which would means CrslPos is 6, I want to display 1.5, but 1.0 gets displayed.

I'm sure the solution is blindingly obvious, but how can I get the .5 to show correctly?

Thanks,
Moray



Group: DynoMotion Message: 15105 From: TK A2 Date: 10/20/2017
Subject: Re: Displaying doubles/floats in a message box
Hi Moray,

For an integer divided by an integer the compiler will code an integer division.   To do floating point division cast one of the operands as floating point. I.e. Change 4 to 4.0. 

HTH 
Regards 
TK

On Oct 20, 2017, at 1:32 PM, moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 

As part of my tool changer initialisation, I display a couple text boxes to confirm positions/tools. However, I can't get partial positions to display properly.

Here's the relevant code snippet-

char MyMessage[80];  // String to be created and displayed
     double CrslP = *CrslPos/4;
     printf("CrslPos = %d\n", *CrslPos);
     printf("CrslP = %.1f\n", CrslP);
     sprintf(MyMessage,"Is carousel at position %.1f",CrslP);  // build the message we want to show
     MsgBoxNoWait(MyMessage,MB_YESNO|MB_ICONEXCLAMATION);
     
*CrslPos is an integer/uservar that contains the carousel position (the numbered position gets multiplied by 4, as it runs on a Geneva mechanism with 2 rotation between each tool holder, and multiplyin by 4 allows me to track position, and use remainder calculations to test for a valid position)

What I'd like is when the carousel is at the mid-point between two holders, I want the message box to show x.5, however the fractional is shown as 0.
I.e. if the carousel is between 1 and 2, which would means CrslPos is 6, I want to display 1.5, but 1.0 gets displayed.

I'm sure the solution is blindingly obvious, but how can I get the .5 to show correctly?

Thanks,
Moray



Group: DynoMotion Message: 15106 From: Moray Cuthill Date: 10/23/2017
Subject: Re: Displaying doubles/floats in a message box
Finally got a chance to edit the code today, and that solved the problem. I knew it would be something simple.

Thanks,
Moray

On Fri, Oct 20, 2017 at 9:45 PM, TK A2 tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Hi Moray,

For an integer divided by an integer the compiler will code an integer division.   To do floating point division cast one of the operands as floating point. I.e. Change 4 to 4.0. 

HTH 
Regards 
TK

On Oct 20, 2017, at 1:32 PM, moray.cuthill@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:

 

As part of my tool changer initialisation, I display a couple text boxes to confirm positions/tools. However, I can't get partial positions to display properly.

Here's the relevant code snippet-

char MyMessage[80];  // String to be created and displayed
     double CrslP = *CrslPos/4;
     printf("CrslPos = %d\n", *CrslPos);
     printf("CrslP = %.1f\n", CrslP);
     sprintf(MyMessage,"Is carousel at position %.1f",CrslP);  // build the message we want to show
     MsgBoxNoWait(MyMessage, MB_YESNO|MB_ICONEXCLAMATION);
     
*CrslPos is an integer/uservar that contains the carousel position (the numbered position gets multiplied by 4, as it runs on a Geneva mechanism with 2 rotation between each tool holder, and multiplyin by 4 allows me to track position, and use remainder calculations to test for a valid position)

What I'd like is when the carousel is at the mid-point between two holders, I want the message box to show x.5, however the fractional is shown as 0.
I.e. if the carousel is between 1 and 2, which would means CrslPos is 6, I want to display 1.5, but 1.0 gets displayed.

I'm sure the solution is blindingly obvious, but how can I get the .5 to show correctly?

Thanks,
Moray